import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;



public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		new Test().test();
	}
	public void test(){
		String url=null;
		client=new DefaultHttpClient();
		// 这一行必须要加，否则服务器无法获取登陆状态
		HttpClientParams.setCookiePolicy(client.getParams(),CookiePolicy.BROWSER_COMPATIBILITY);
		
			try {
				
				Document document=getDocument(url,"learn.tsinghua.edu.cn");
			} catch (Exception e) {
				System.err.println("crawler run err."+e.getMessage());;
				e.printStackTrace();
			}
		
		client.getConnectionManager().closeExpiredConnections();
	}
	/**
	 *  用httpclient太慢了 弃用
	 */
	HttpClient client;
	
	/**
	 * 使用HttpClient的版本  
	 *  太慢了 弃用
	 * @param url
	 * @param baseUrl
	 * @return
	 */
	private Document getDocument(String url,String baseUrl){
		try {
			//HttpGet httpget = new HttpGet("");
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("userid", "huangxd12"));
			formparams.add(new BasicNameValuePair("userpass", "hhxxdd1989"));
			//formparams.add(new BasicNameValuePair("submit1", "登录"));
			
			//userid=huangxd12&userpass=hhxxdd1989&submit1=%B5%C7%C2%BC
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
			HttpPost httpPost=new HttpPost("https://learn.tsinghua.edu.cn/MultiLanguage/lesson/teacher/loginteacher.jsp");	
			httpPost.setEntity(entity);
			
			HttpResponse response = client.execute(httpPost);
			//String cookie = response.getFirstHeader("set-Cookie").getValue();
			
			HttpEntity responseEntity = response.getEntity();
			if (responseEntity != null) {		
				System.out.println("--------------------------------------");
				System.out.println("Response content: " + EntityUtils.toString(responseEntity,"UTF-8"));
				System.out.println("--------------------------------------");
			}
			HttpGet httpget = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/lesson/student/MyCourse.jsp?language=cn");
			response = client.execute(httpget);
			responseEntity = response.getEntity();
			if (responseEntity != null) {		
				System.out.println("--------------------------------------");
				System.out.println("Response content: " + EntityUtils.toString(responseEntity,"UTF-8"));
				System.out.println("--------------------------------------");
			}
			
			 httpget = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/lesson/student/course_locate.jsp?course_id=96244");
			response = client.execute(httpget);
			responseEntity = response.getEntity();
			if (responseEntity != null) {		
				System.out.println("--------------------------------------");
				System.out.println("Response content: " + EntityUtils.toString(responseEntity,"UTF-8"));
				System.out.println("--------------------------------------");
			}
			
			///MultiLanguage/lesson/student/download.jsp?course_id=96244
			 httpget = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/lesson/student/download.jsp?course_id=96244");
				response = client.execute(httpget);
				responseEntity = response.getEntity();
				if (responseEntity != null) {		
					System.out.println("--------------------------------------");
					System.out.println("Response content: " + EntityUtils.toString(responseEntity,"UTF-8"));
					System.out.println("--------------------------------------");
				}
				///uploadFile/downloadFile_student.jsp?module_id=322&filePath=5UfHsSwANWiGer3moj%2Bwvb02gTWumEOBYiJKizsarWu4h/d64M2JV4gpqKr2hrAOvw3uGd777Ro%3D&course_id=96244&file_id=850620
				 httpget = new HttpGet("http://learn.tsinghua.edu.cn/uploadFile/downloadFile_student.jsp?module_id=322&filePath=5UfHsSwANWiGer3moj%2Bwvb02gTWumEOBYiJKizsarWu4h/d64M2JV4gpqKr2hrAOvw3uGd777Ro%3D&course_id=96244&file_id=850620");
					response = client.execute(httpget);
					responseEntity = response.getEntity();
					if (responseEntity != null) {
						Header header =response.getFirstHeader("Content-Disposition");
						
						//responseEntity.
						//System.out.println("-"+);
						//attachment;filename="C.0.Overview_480807981.ppt"
						System.out.println("--------------------------------------");
						downloadFile(responseEntity.getContent(),"e:/tmp/",header.getValue().substring(21,header.getValue().length()-1));
						//System.out.println("Response content: " + EntityUtils.toString(responseEntity,"UTF-8"));
						System.out.println("--------------------------------------");
					}
					
					
//			if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
//				
//				Document document=Jsoup.parse(EntityUtils.toString(response.getEntity(), "utf-8"),baseUrl);
//				return document;
//			}else{
//				System.err.println("connect err:"+url+"("+response.getStatusLine());
//			}
			//return Jsoup.connect(url).get();
		} catch (IOException e) {
			System.err.println("connect err:"+url+"("+e.getMessage());
			//e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 
	 * @param input
	 * @param Folder 需要以/结尾
	 * @param fileName 
	 * @return
	 * @throws IOException
	 */
    public static void  downloadFile(InputStream input,String Folder,String fileName) throws IOException{
           
            
            File   file   =   new   File( Folder+fileName); 
            file.getParentFile().mkdirs();
            FileOutputStream out=new FileOutputStream(file); 
            IOUtils.copy(input, out);
            //                      int   i=0; 
            //                      while   ((i=is.read())!=-1)   { 
            //                              out.write(i); 
            //                      } 
           
            out.close();
           
    }
}
